package com.zeng.createmd.restfulmodule.util;

import javax.websocket.RemoteEndpoint;
import javax.websocket.Session;
import java.io.IOException;

public class WebSocketUtil {

    private static Session session;

    public static void sendMessage(String message) {
        if (session == null) {
            throw new RuntimeException("尚未链接客户端");
        }
        RemoteEndpoint.Basic basic = session.getBasicRemote();
        if (basic == null) {
            return;
        }
        try {
            basic.sendText(message);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public static void setSession(Session session) {
        WebSocketUtil.session = session;
    }

    public static void closeSession() {
        try {
            if (session != null) {
                session.close();
                session = null;

            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
